package com.ruoyi.system.mapper;

import com.ruoyi.common.core.dao.BaseMapper;
import com.ruoyi.common.entity.DO.marketing.CouponDO;
import com.ruoyi.common.entity.DO.product.ProductCategoryDO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;


/**
 * 商品分类 数据层
 *
 * @author lijiantao
 */
public interface IAsyncShopCouponMapper extends BaseMapper<CouponDO> {

    @Update("UPDATE coupon set total_get=(SELECT COUNT(1) FROM member_coupon where coupon_id=#{couponId}) WHERE id=#{couponId}")
    int refreshTotalGet(Long couponId);

    @Update("UPDATE coupon set total_use=(SELECT COUNT(1) FROM member_coupon where coupon_id=#{couponId} and member_coupon_status=1) WHERE id=#{couponId}")
    int refreshTotalUse(Long couponId);

    @Update("UPDATE coupon set stock=total_issuance-total_get WHERE id=#{couponId}")
    int refreshStock(Long couponId);

    @Update("UPDATE coupon SET coupon_status = 0 WHERE is_delete=0 and coupon_status=1 and NOW() < use_start_time")
    int waitGetCoupon();
    
    @Update("UPDATE coupon SET coupon_status = 1 WHERE is_delete=0 and coupon_status=0 and NOW() BETWEEN use_start_time AND use_end_time")
    int nowGettingCoupon();

    @Update("UPDATE coupon SET coupon_status = 2 WHERE is_delete=0 and coupon_status=1 and stock=0")
    int getOverCoupon();

    @Update("UPDATE coupon SET coupon_status = 3 WHERE is_delete=0 and coupon_status in (1,2) and NOW() > use_end_time")
    int expireCoupon();

    @Update("UPDATE member_coupon SET member_coupon_status = 2 WHERE member_coupon_status=0 and coupon_id in(SELECT id from coupon where is_delete=0 and coupon_status=3)")
    int expireMemberCoupon();
}
